package com.bjpowernode.singleton;

import java.util.concurrent.CountDownLatch;

/**
 * @ClassName TestSingleton
 *
 * @Author Cat
 */
public class TestSingleton {

    public static void main(String[] args) throws Exception {
        long start = System.currentTimeMillis();

        int num = 100;
        CountDownLatch countDownLatch = new CountDownLatch(num); //倒计数器

        for (int i = 0; i < num; i++) {
            new Thread(() -> {
                // 线程阻塞，直到计数器变为0，才会继续往下执行
                try {
                    countDownLatch.await();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }

                //1.测试饿汉式
                //Singleton01 singleton01 = Singleton01.getInstance();
                //System.out.println(singleton01);

                //1.测试懒汉式
                Singleton08 singleton03 = Singleton08.INSTANCE;
                System.out.println(singleton03);
                System.out.println(singleton03.sayHi());


            }).start();
            //倒计数减一
            countDownLatch.countDown();
        }

        /*long end = System.currentTimeMillis();
        System.out.println("总耗时：" + (end - start));*/
    }
}